
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import util.DbUtil
import domain.Athlete

class Main {
	private final static Logger log = LoggerFactory.getLogger(Main.class)

	static main(args) {
		// 准备工作
		DbUtil.prepare()
		// get sql
		def sql = DbUtil.getSql()

		log.info('Insert an athlete ...')
		sql.execute('''
insert into t_athlete (firstname, lastname, birthday) 
values('zhao', 'bo', '1986-05-29');
''')
		sql.close()
		log.info('Inserting done.')
		

		// 使用 prepared statement 
		log.info('Insert an athlete using prepared statement ...')
		sql = DbUtil.getSql()
		def athleteInsert = '''
insert into t_athlete(firstname, lastname, birthday) values(?, ?, ?);
'''
//		def insertValue = ['d', 'jh', '1988-03-22']
		def insertValue = ['d', 'jh', Date.parse('yyyy-MM-dd', '1988-03-22')]
		sql.execute athleteInsert, insertValue
		sql.close()
		log.info('Inserting done.')
		
		// 使用 placeholder statement
		log.info('Insert an athlete using placeholder statement ...')
		def athlete = new Athlete(firstname: 'b', lastname: 'b', 
			birthday: Date.parse('yyyy-MM-dd', '2017-11-11'))

		sql.execute """
insert into t_athlete(firstname, lastname, birthday) 
values(${athlete.firstname}, ${athlete.lastname}, ${athlete.birthday});
"""

		sql.close()
		log.info('Inserting done.')
	}
}